package cn.wolfcode.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.xmlunit.builder.Input;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Slf4j
@Component
public class OSSUtil {
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyID}")
    private String accessKeyID;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    public String getURL(MultipartFile multipartFile) throws IOException {
        InputStream inputStream = multipartFile.getInputStream();
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + suffix;
        OSS ossClient = new OSSClient(endpoint, accessKeyID, accessKeySecret);
        ossClient.putObject(bucketName, newFileName, inputStream);
        StringBuffer stringBuffer = new StringBuffer(endpoint);
        String resultURL = stringBuffer.insert(8, bucketName + ".").append("/").append(newFileName).toString();
        return resultURL;
    }

}

//OSS ossClient = new OSSClient(properties.getEndpoint(), properties.getAssessKeyId(), properties.getAssessKeySecretId());
//        ossClient.putObject(properties.getBucketName(), newFileName, inputStream);
//StringBuffer sb = new StringBuffer(properties.getEndpoint());
//        return sb.insert(8, properties.getBucketName() + ".").append("/").append(newFileName).toString();